#coding=utf-8
import requests,json,urllib
import re
import mysql.connector
import connect_mysql

#引用connect_mysql程序连接数据库
connect_mysql.connect_mysql()

def cw_shangzhang(url,adminname,phone,password,username,money):
    import time, datetime
    try:
        login_headers = {'Referer': url, 'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
                            'Upgrade-Insecure-Requests': '1', 'Content-Type': 'application/x-www-form-urlencoded',
                            'Accept-Encoding': 'gzip, deflate',
                            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0',
                            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'}
        s = requests.session()
        login_data = {'adminname':adminname,'phone':phone,'password':password,'authCode':'123456'}
        login = s.post(url, data=login_data,headers=login_headers,verify=False)
        #print login.text
        login1 = str(login)
        if u"登录成功" in login.text:
            print login1 + u"登录成功"
            #登录成功，进入上账操作
            # 折中方案，参数按如下方式组织，也是模拟multipart/form-data的核心
            shangzhang_data = {'username':(None,username),'money':(None,money),'remark':(None,'上账')}
            shangzhang = s.post(url+'/deposit/depositsub/',data=shangzhang_data,cookies=login.cookies,headers=login_headers,verify=False)
            #print shangzhang.text
            #进行初审
            search_data = {'username':username,'channel':'-1'}
            search = s.get(url+'/deposit/record/',params=search_data,cookies=login.cookies,headers=login_headers,verify=False)
            res_url = r"(?<=href=\").+?(?=\")|(?<=href=\').+?(?=\')"
            id_url = re.findall(res_url, search.text, re.I | re.S | re.M)  # 取出所有url
            # 循环遍历取出的所有url，并通过判断获得id值
            for id1 in id_url:
                arr = id1.split(',')
                if '/deposit/verifyform/?id=' in arr[0]:
                    break
            print str(id1)
            id = re.findall(r'(\w*[0-9]+)\w*', id1)  # 从字符串中取出id值
            print id
            examine01_data = {'id':id,'status':'1','verify_remark':u'审核通过'}
            examine01 = s.post(url+'/deposit/verify/',data=examine01_data,cookies=login.cookies,headers=login_headers,verify=False)
            examine02 = s.post(url+'/deposit/verify/',data=examine01_data,cookies=login.cookies,headers=login_headers,verify=False)
            now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))  # 数据产生日期
            # 进行数据库写入
            connect = connect_mysql.connect_mysql()[0]  # connect是由connect_mysql函数返回的第一个值
            cursor = connect.cursor()
            # SQL 插入语句
            sql = "insert into mysql.shangzhang(username,money,modify_time) values('%s','%s','%s')" % (username,money, now)
            # 执行sql语句
            input = cursor.execute(sql)
            connect.commit()  # 实例提交命令
            # cursor.close()
            # connect.close()
            print("insert ok")
            return u"上账成功，上账金额为：" + money + u"，上账用户为：" + username

        else:
            return u"登录失败！请检查账号信息是否正确！"
    except:
        return u"上账失败！请仔细检查所填信息后再次提交！"